#!/bin/sh

###############################################
# File:         mvnraw
# Description:  验证给定的路径，
#               若存在，则返回1；
#               否则返回0，并邮件告知
# ie:
#                           /home/working/mvnsrv.sh -a common-info -s assembly -d deploy -t online
#
# Usage:        sh mvnraw \
#               -a app need to check \
#               [-b build js \]
#               [-d deploy web \]
#               [-s assembly srv \]
#
# author:       riverbo@126.com
# date:         2016-10-31
###############################################

while getopts "g:" opt
do 
    case $opt in
	g) #set option "g"
            branch=$OPTARG
            ;;
        *)
            echo "-$opt not recognized"
            ;;
    esac
done

declare path=$(cd `dirname $0`; pwd)
echo "current path is  :" ${path}  

#删除本地仓库缓存jar
${path}/flush-jar.sh

declare appName=common-info;
declare appPath=${path}/${appName}

cd ${appPath}
echo "current path is  :" ${appPath}  

#echo "rm start..."
rm -rf /root/.m2/repository/com/lbiuse/
rm -rf /root/.m2/repository/org/bidtime/utils/
#echo "rm end."


#echo "git pull start..."
if [ -n "${branch}" ]; then
  echo "git 分支 ${branch}"
	git checkout ${branch}
  git pull origin ${branch}
  else
  echo "git 分支 master"
  git checkout dev
  git pull dev
fi

#echo "git pull end."


declare DEPLOY_DIR="/home/apps/server";
declare zipFile=${appName}-server-1.0-SNAPSHOT-cli.tar.gz;
declare source_zipfile=${appPath}/server/target/${zipFile}

echo "mvn clean install start..."
mvn clean install -q
# mvn clean install -Dmaven.test.skip=true
echo "mvn clean install  end."

echo "assembly start..."
    declare server_sub_dir="server";
    if [ -d "${appPath}/${server_sub_dir}" ]; then
      #  cd server sub dir
      echo "进入server建包:" ${server_sub_dir}
      cd ${server_sub_dir}
      mvn assembly:assembly -q
      cd ..
    fi
echo "assembly end."
	
if [ ! -f "${source_zipfile}" ]; then
  echo "zip文件不存在,${source_zipfile},exit"
  exit 0;
fi


# deploy  ----------------------------------------------

declare DEPLOY_APP_PATH="${DEPLOY_DIR}/${appName}";
echo "${appName} starting deploy..."
echo ""

declare deploy_arch_path=${DEPLOY_APP_PATH}/archv;
if [ ! -d "${deploy_arch_path}" ]; then
    echo "目录不存在,${deploy_arch_path}，创建目录"
    mkdir -p ${deploy_arch_path}
fi

# prepare to copy
declare deploy_zipfile=${deploy_arch_path}/${zipFile}

# test
if [ "${test}"x == "online"x ]; then
    echo "cp to remote ${test}"
    scp ${source_zipfile} root@122.114.80.44:${deploy_arch_path}
else
  if [ -f "${deploy_zipfile}" ]; then
      echo "zip文件存在,${deploy_zipfile},delete it";
      rm -rf ${deploy_zipfile};
  fi

  echo "cp to local ${test}"
  # cp
  mv -f ${source_zipfile} ${deploy_zipfile}

  # file is exists
  if [ ! -f "${deploy_zipfile}" ]; then
    echo "  源 ${deploy_zipfile} 不存在, 退出."
    exit 0;
  else
    echo "  源 ${deploy_zipfile} 存在, 可以继续."
    echo " ok it."
    echo " "
    echo "-------------------------------------------- "
  fi

fi

# deploy
if [ "${deploy}"x != "deploy"x ]; then
  echo " deploy is empty, don't need deploy, exit."
  cd ${path}
  exit 0
fi

echo "deploy ${appName}"

if [ "${test}"x == "online"x ]; then
    echo "deploy remote ${test}, don't deploy, exit."
else
    echo "deploy local ${test}"
    ${DEPLOY_DIR}/deploy.sh ${appName}
fi

echo "-------------------------------------------- "

echo "${appName} end deploy."

echo "返回" ${path}
cd ${path}


